package com.wsq.deviceinfo;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresApi;

import java.util.UUID;

/**
 * Date:2023/3/27
 * Time:16:31
 * author:wushengqi
 *
 * 设备识别号：
 * 获得权限前：UUID并持久化
 * 获得权限后：AndroidID、手机号、UUID
 * 低版本：MAC、IMEI
 */
public class DeviceUtils {
    @RequiresApi(api = Build.VERSION_CODES.O)
    private String getDeviceId(Context context) {

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        }else{
//            String ss = tm.getImei();
        }
        return "";
    }

    public static String getDeviceId() {

        String serial = null;
        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 位

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                serial = Build.SERIAL;
            } else {
                serial = Build.SERIAL;
            }
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        }
        catch (Exception exception)
        {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }

    public static String getAndroidID(Context context){
        return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static String getSN(Context context){
        return Build.SERIAL;
    }


    public static String getImei(Context context) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            return null;
        } else {
            try {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                @SuppressLint("MissingPermission") String deviceId = telephonyManager.getDeviceId();
                return deviceId;
            } catch (Exception e) {
                return null;
            }
        }
    }

}
